<?php
/**
 * WpTestSuite.php
 * 
 * This file contains the WpTestSuite class, a class to help for testing
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage util
 */

/**
 * This class provides functionality to create a simple testsuite. Since it is abstract, 
 * it must be extended and cannot be used itself. 
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage util
 * @abstract
 */
abstract class WpTestSuite
{
  /**
   * Error counter
   */
  private $_err = 0;
  
  /**
   * Success counter
   */
  private $_suc = 0;
  
  /**
   * Prints out a message
   */
  public function addMessage($msg)
  {
     print('<tr class="message">'."\n".
      '<td>'.$msg.'</td>'."\n"
    );
  }
  
  /**
   * Adds a test result to the result list
   * @param string $title The title of the test
   * @param mixed $expected The expected value
   * @param mixed $is The value as it is
   * @param string $cmptype The compare type ('===', '!==', '<', '>', '<=', '>=', '',
   *  'string', 'int', 'bool', 'null', 'count', 'array')
   */
  public function addResult($title, $expected, $is, $cmptype='===')
  {
    $class = 'success';
    
    $cmpval = true;
    $expval = $expected;
    $isval = $is;
    switch ($cmptype) {
      case '!==': $cmpval = $expected !== $is; break;
      case '<': $cmpval = $expected < $is; break;
      case '>': $cmpval = $expected > $is; break;
      case '<=': $cmpval = $expected <= $is; break;
      case '>=': $cmpval = $expected >= $is; break;
      case '': $cmpval = $expected === ''; break;
      case 'string': $cmpval = is_string($is); $expval='is_string'; break;
      case 'int': $cmpval = is_int($is); $expval='is_int'; break;
      case 'bool': $cmpval = is_bool($is); $expval='is_bool'; break;
      case 'null': $cmpval = is_null($is); $expval='is_null'; break;
      case 'array': $cmpval = is_array($is); $expval='is_array'; break;
      case 'count': $cmpval = (is_array($is) && count($is) === $expected); $is=count($is); break;
      default: $cmpval = $expected === $is; break;
    }
    
    if (!$cmpval) {
      $this->_err++;
      $class = 'error';
    } else {
      $this->_suc++;
    }
    
    print('<tr class="'.$class.'">'."\n".
      '<td>'.$title.'</td><td>'.var_export($expval, true).'</td><td>'.$cmptype.'</td><td>'.var_export($is, true).'</td>'."\n".
      '</tr>'."\n"
    );
    
  }
  
  /**
   * The test method needs to be overwritten. Put all tests in here
   */
  public abstract function test();

  /**
   * This method will be called before testing. You can do database init and stuff like this in here
   */
  public abstract function prepare();
  
  /**
   * This method is going to be called after testing
   */
  public abstract function cleanup();

   
  /** 
   * Starts the tests
   * @param string $title The title of the test serie 
   */
  public function start($title)
  {
    print('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'."\n". 
      '<html>'."\n".
      '<head>'."\n".
      '<title>'.$title.'</title>'."\n".
      '<style type="text/css">'."\n".
      'tr.success td { color: green; }'."\n".
      'tr.error td { color: red; }'."\n".
      'tr.message td { color: #777; }'."\n".
      '</style>'."\n".
      '</head>'."\n".
      '<body>'."\n".
      '<h2>Starting preparation for: '.$title.'</h2>'."\n".
      '<table>'."\n"
     );
     
     if (!$this->prepare()) {
       return;
     }
     
     print('</table>'.
      '<h2>Starting tests for: '.$title.'</h2>'."\n".
      '<table>'."\n".
      '<tr><th>Test (or info txt)</td><td>Expected</td><td>Comparer</td><td>Value</td></tr>'."\n"
     );
     
     $this->test();
     
     print('</table>'."\n".
      '<h1>OK: '.strval($this->_suc).' / ERRORS:'.strval($this->_err).'</h1>'."\n".
      '<h2>Starting cleanup for: '.$title.'</h2>'."\n".
      '<table>'."\n"
     );
     
     $this->cleanup();

     print('</table>'."\n".'</body>'."\n".'</html>');
  }
}

/* End of file WpTestSuite.php */
/* Location: ./wildpack/util/WpTestSuite.php */ 
